package com.jediflasher.game_server.core.events

import com.jediflasher.game_server.util.log.Logger
import collection.mutable.{HashSet, HashMap}

/**
 * User: Obi
 * Date: 14.08.11
 * Time: 11:03
 */

trait EventDispatcher {

	val targetsHash = new HashMap[String, HashSet[Event => Unit]];

	def dispatchEvent(event:Event) {
		event.target = this;
	
		val eventType = event.getType;
		if(targetsHash.contains(eventType)) {
			targetsHash(eventType).foreach(_.apply(event))
		}
	}

	def addListener(eventType:String, target:Event => Unit) {
		if(!targetsHash.contains(eventType)) {
			targetsHash += eventType -> new HashSet[Event => Unit]();
		}
		
		targetsHash(eventType) += target;
	}
}

